package com.tupu.controller.admin;

import cn.hutool.http.server.HttpServerResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tupu.common.Result;

import com.tupu.entity.admin.KnowledgeInfo;
import com.tupu.service.admin.KnowledgeListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/knowledgeList")
public class HomePageController {

    @Autowired(required = false)
    private KnowledgeListService knowledgeListService;

    /**
     * 首页展示分页查询 根据名字模糊搜索
     *
     * @param page
     * @param size
     * @param name
     * @return
     */
    @PostMapping("/list")
    public Result<Page<KnowledgeInfo>> searchByName(@RequestParam(required = false, defaultValue = "1") Long page,
                                                    @RequestParam(required = false, defaultValue = "10") Long size,
                                                    @RequestParam(required = false) String name) {
        try {
            Page<KnowledgeInfo> pageInfo = knowledgeListService.pageInfo(name, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 获取所有图谱，不分页
     *
     * @return
     */
    @PostMapping("/listAll")
    public Result<List<KnowledgeInfo>> findAll() {
        List<KnowledgeInfo> knowledgeInfos = knowledgeListService.listAll();
        return Result.ok(knowledgeInfos);
    }

    /**
     * 新建图谱
     *
     * @param knowledgeInfo
     * @return
     */
    @PostMapping("/save")
    public Result<KnowledgeInfo> add(KnowledgeInfo knowledgeInfo) {
        try {
            knowledgeListService.addKnowledge(knowledgeInfo);
            return Result.ok(knowledgeInfo);
        } catch (Exception e) {
            return Result.errorMessage("新增图谱失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 编辑图谱（目前未用到）
     *
     * @param knowledgeInfo
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody(required = false) KnowledgeInfo knowledgeInfo) {
        try {
            knowledgeListService.updateKnowledge(knowledgeInfo);
            return Result.ok();
        } catch (Exception e) {
            return Result.errorMessage("编辑图谱失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 根据id删除图谱(可以批量删除)
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public Result del(@RequestParam(value = "id") List<Integer> ids) {
        try {
            for (Integer id : ids) {
                knowledgeListService.deleteByNodeId(id);
            }
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage("删除失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 根据id查找一个图谱
     *
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result<KnowledgeInfo> selectById(@RequestParam(required = false) Integer id) {
        try {
            KnowledgeInfo knowledge = knowledgeListService.findById(id);
            return Result.ok(knowledge);
        } catch (Exception e) {
            return Result.errorMessage("查询失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Map<String, Object> UploadPicture(@RequestParam(required = false) MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = knowledgeListService.uploadPicture(file);
            return result;
        } catch (IOException e) {
            result.put("上传失败，错误信息:", e.getMessage());
            return result;
        }
    }

    /**
     * 图谱备份
     *
     * @param node_id
     * @return
     * @throws IOException
     */
    @PostMapping("/backup")
    public Result<Map<String, Object>> backupByNodeId(@RequestParam(required = false) Integer node_id, HttpServerResponse response) {
        try {
            Map<String, Object> result = knowledgeListService.backup(node_id,response);
            return Result.ok(result);
        } catch (IOException e) {
            return Result.errorMessage("备份失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 图谱批量备份
     *
     * @param ids
     * @return
     * @throws IOException
     */
    @PostMapping("batchBackup")
    public Result<Map<String, Object>> backupByNodeId(@RequestParam(required = false) List<Integer> ids) {
        for (Integer id : ids) {
        }
        return Result.ok();
    }
}